<template>
  <div class="examinexaminationsWrapWrap">
    <div class="pageTableInner">
      <div class="infoSearchWrap">
        <div class="infoSearchOne">
          <el-row :gutter="20">
            <el-col :span="10">
              <div class="searchWrap">
                <el-row :gutter="20">
                  <el-col :span="4">
                    <span class="searchFont">企业选择：</span>
                  </el-col>
                  <el-col :span="20">
                    <el-radio-group v-model="checkEnterPriseNo" size="mini" @change="getCurrentRows()">
                      <el-radio-button v-for="item in companyInfos" :label="item.enterpriseNo"
                                       :key="item.enterpriseName">{{item.enterpriseName}}
                      </el-radio-button>
                    </el-radio-group>
                  </el-col>
                </el-row>
              </div>
            </el-col>

            <div style="float: right;margin-right: 10px;" >
              <span v-if="$store.state.btnMenuIds.indexOf(77)>-1">
              <span  v-show="!hasCheck">
              <el-button type="primary" size="mini" @click="handleSave(1)">保存</el-button>
              <el-button type="primary" size="mini" @click="handleSave(2)">提交考核结果</el-button>
               </span>
                </span>
              <router-link to="/entering">
                <i class="iconfont icon-fanhui"  style="font-size: 20px;color: #3975FE;cursor: pointer;margin-left: 20px;"></i>
              </router-link>
            </div>
          </el-row>
        </div>
      </div>

      <div class="tableContainer">
        <div class="tableInner" ref="tableInner">
          <el-table
            ref="multipleTable"
            :data="rows"
            stripe
            :height="tableHeight"
            :span-method="objectSpanMethod"
            tooltip-effect="dark"
            style="width: 100%"
          >
            <el-table-column
              label="指标类别"
              width="120"
              prop="classifyName"
            >
            </el-table-column>
            <el-table-column
              width="80"
              label="类别分值"
              prop="classifyScore"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="itemsName"
              label="指标项"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              width="80"
              prop="standardScore"
              label="指标分值"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              label="指标描述"
              prop="itemsDesc"
              show-overflow-tooltip>

            </el-table-column>
            <el-table-column
              label="评分标准"
              prop="scoreRule"
              show-overflow-tooltip
              >
              <template slot-scope="scope">
                <div style="float: left;">
                  {{scope.row.scoreRule}}
              </div>

              </template>
            </el-table-column>
            <el-table-column
              label="评分"
              prop="finalScore"
              width="100"
            >
              <template slot-scope="scope">
                <el-input class="tableInput" size="mini" type="number" @change="getstatistics(scope.row)"
                          v-model="scope.row.finalScore"
                          autocomplete="off" placeholder="数字"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="100">
              <template slot-scope="scope">
                <el-checkbox @change="getstatistics" :disabled="$store.state.userInfo.roleLevel!=1" v-model="scope.row.hasExamine" :true-label="1" :false-label="0">参与考核</el-checkbox>
              </template>
            </el-table-column>
            <el-table-column
              label="附件管理"
              width="120"
              >
              <template slot-scope="scope">
                <div v-show="scope.row.hasAttachment" >
                  <span   style="cursor: pointer;" @click="downLoadXml(scope.row.relativeAddr)">{{getFileName(scope.row.attachmentAddr)}}</span>
                  <div v-if="$store.state.btnMenuIds.indexOf(71)>-1" @click="$refs.uploadDialog.dialogOpen(scope.row)" style="color:#3975FE; cursor: pointer;margin-bottom: 5px;"><i class="iconfont icon-24"></i>上传附件
                  </div>
                </div>
                <span v-show="!scope.row.hasAttachment">无需附件</span>
              </template>
            </el-table-column>
          </el-table>
          <div style="line-height: 40px;border-bottom: 1px solid #ebeef5;color: grey;text-align: center;">
            <el-row>
              <!--<el-col :span="4">总分：{{totalScore}} 分</el-col>-->
              <el-col :span="4">考核指标：{{rows.length}} 项</el-col>
              <el-col :span="4">已考核：{{getInfo.hasExamine}} 项</el-col>
              <el-col :span="4">未考核：{{getInfo.noExamine}} 项</el-col>
              <el-col :span="4">不参与考核：{{getInfo.outdrop}} 项</el-col>
              <el-col :span="4">应上传附件：{{getInfo.uploadDialog}} 项</el-col>
              <el-col :span="4">未上传：{{getInfo.nouploadDialog}} 项</el-col>
            </el-row>
          </div>
        </div>
      </div>
    </div>
    <uploadDialog ref="uploadDialog"  @subSave="subSave"></uploadDialog>
  </div>
</template>

<script>
  import uploadDialog from "./uploadDialog.vue"
  export default {
    props: [],
    data(){
      return {
        allRows: [],
        rows: [],
        spanArr: [],
        companyInfos: [],
        agencyId: 1,
        action: 1, //1：保存 2：提交考核
        checkEnterPriseNo: "075501",
        tableHeight: 400,
        totalScore: 0,
        hasCheck: true,
        templateId: "",
        taskId: "",
        getInfo: {
          hasExamine: 0,
          noExamine: 0,
          outdrop: 0,
          uploadDialog: 0,
          nouploadDialog: 0,
        }
      }
    },
    components: {uploadDialog},
    created(){
     this.changecheckInfo()

    },
    mounted(){

      this.$nextTick(() => {
        //表格滚动
        this.tableHeight = document.querySelector(".examinationWrap").clientHeight - 160
      })
    },

    methods: {
      //下载模板
      downLoadXml(downloadPath){
        window.location.href = downloadPath
      },
      //获取所有模板信息
      changecheckInfo(){
        this.axios.post(process.env.API_HOST + `/examine/selectTask`, {})
          .then(response => {
            const result = response.data
            if (result.code == 200) {
              this.examineTask = result.result.examineTask
              this.taskId = this.examineTask.id
              this.templateId=this.examineTask.templateId
              this.getExamineInfo()
              let ispass=true
              this.hasCheck=true
              result.result.notAgencyTask.forEach(item=>{
                if(ispass){
                  if(this.$store.state.agencyId==item.agencyId){
                    ispass=false
                    this.hasCheck=false
                  }else {
                    this.hasCheck=true
                  }
                }

              })

            }
          })
      },
      subSave(){
        let detailList = [],isFullScore=true,isFulluploadDialog=true
        this.allRows.forEach(item=>{
          item.rows.forEach(item1=>{
            if(item1.hasExamine&&(item1.finalScore===""||item1.finalScore===null)){
              isFullScore=false
            }
            if(item1.hasAttachment&&!item1.attachmentAddr){
              isFulluploadDialog=false
            }
            detailList.push({
              id:item1.id,
              finalScore:item1.finalScore,
              hasExamine:item1.hasExamine,
              attachmentAddr:item1.attachmentAddr,
            })
          })
        })
        this.axios.post(process.env.API_HOST + `/examine/submitAgencyResult`, {
          templateId: this.templateId,
          agencyId: this.$store.state.agencyId,
          taskId: this.taskId,
          action: action,
          detailList: detailList,
        })
          .then(response => {
            const result = response.data
            if (result.code === 200) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
              this.getExamineInfo()
            }
          })
      },
      handleSave(action){
        let msg=action==1?"确认保存所有企业考核":"确认提交所有企业的考核结果"
        let detailList = [],isFullScore=true,isFulluploadDialog=true
        this.allRows.forEach(item=>{
          item.rows.forEach(item1=>{
            if(item1.hasExamine){
              if(item1.finalScore===""||item1.finalScore===null){
                isFullScore=false
              }
              if(item1.hasAttachment&&!item1.attachmentAddr){
                isFulluploadDialog=false
              }
            }

            detailList.push({
              id:item1.id,
              finalScore:item1.finalScore,
              hasExamine:item1.hasExamine,
              attachmentAddr:item1.attachmentAddr,
            })
          })
        })
        if(action==2&&!isFullScore){
          this.$message({
            type: 'warning',
            message: "还有未评的评分"
          });
          return
        }else if(action==2&&!isFulluploadDialog){
          this.$message({
            type: 'warning',
            message: "还有附件未上传"
          });
          return
        }
        this.$confirm(msg+', 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.axios.post(process.env.API_HOST + `/examine/submitAgencyResult`, {
            templateId: this.templateId,
            agencyId: this.$store.state.agencyId,
            taskId: this.taskId,
            action: action,
            detailList: detailList,
          })
            .then(response => {
              const result = response.data
              if (result.code === 200) {
                this.$message({
                  type: 'success',
                  message: '操作成功!'
                });
                if(action==2){
                  this.$router.push("/entering")
                  this.changecheckInfo()
                }
                this.getExamineInfo()
              }
            })
        }).catch(() => {

        });
      },
      //获取所有信息
      getExamineInfo(){
        this.axios.post(process.env.API_HOST + `/examine/selectAgencyDetail`, {
          templateId: this.templateId,
          agencyId: this.$store.state.agencyId,
          taskId: this.taskId,
        })
          .then(response => {
            let result = response.data
            if (result.code == 200) {
              this.companyInfos=[]
              result.result.forEach((data, key) => {
                this.companyInfos.push({enterpriseNo:data.enterpriseNo,enterpriseName:data.enterpriseName})
                this.allRows[key]={}
                this.allRows[key].enterpriseNo = data.enterpriseNo
                this.allRows[key].enterpriseName = data.enterpriseName
                this.allRows[key].enterpriseId = data.enterpriseId
                this.allRows[key].rows = []
                data.classifyList.forEach(item => {
                  item.itemList.forEach(item1 => {
                    this.allRows[key].rows.push({
                      ...item1,
                      classifyId: item.classifyId,
                      classifyName: item.classifyName,
                      classifyScore: "",
                      hasExamine: item1.hasExamine===null?1:item1.hasExamine
                    })
                  })
                })
              })
              this.getCurrentRows()
            }
          })

      },
      getCurrentRows(){
        this.rows=[]
        let hasExamine = false
        this.allRows.forEach(item=>{
          if(item.enterpriseNo == this.checkEnterPriseNo){
            this.rows = item.rows
          }
        })
        if (this.rows.length > 0) {
          this.taskId = this.rows[0].taskId
          this.spanArr = this.getSpanArr(this.rows, "classifyName")
          this.rows.forEach((item, key) => {
            this.totalScore += item.standardScore
            this.getclassifyScore(key)
          })
          this.getstatistics()
        }else {
          this.getInfo = {
            hasExamine: 0,
            noExamine: 0,
            outdrop: 0,
            uploadDialog: 0,
            nouploadDialog: 0,
          }
        }
      },
      getstatistics(row){
        if(row&&row.finalScore>row.standardScore){
          this.$message({
            type: 'warning',
            message: "评分结果不能大于指标分值"
          });
          row.finalScore=row.standardScore
        }else if(row&&row.finalScore<0){
          this.$message({
            type: 'warning',
            message: "评分结果不能小于零"
          });
          row.finalScore=0
        }
        this.getInfo = {
          hasExamine: 0,
          noExamine: 0,
          outdrop: 0,
          uploadDialog: 0,
          nouploadDialog: 0,
        }
        this.rows.forEach(item => {
          if(item.hasExamine){
            if (item.finalScore) {
              item.finalScore = this.toDecimal(item.finalScore)
              this.getInfo.hasExamine += 1
            } else {
              this.getInfo.noExamine += 1
            }
          }
          if (!item.hasExamine) {
            this.getInfo.outdrop += 1
          }
          if (item.hasAttachment) {
            this.getInfo.uploadDialog += 1
            if (!item.attachmentAddr) {
              this.getInfo.nouploadDialog += 1
            }
          }
        })
      },
      objectSpanMethod({row, column, rowIndex, columnIndex}) {
        if (columnIndex === 0 || columnIndex === 1) {
          const _row = this.spanArr[rowIndex];
          const _col = _row > 0 ? 1 : 0;
          return {
            rowspan: _row,
            colspan: _col
          };
        }
      },
      getclassifyScore(index){
        let sub = 0, isPass = true, classifyScore = 0
        this.spanArr.forEach(item => {
          sub += item
          if (isPass && sub > index) {
            isPass = false
            const sliceArr = this.rows.slice(sub - item, sub)
            sliceArr.forEach(item => {
              classifyScore += this.toDecimal(item.standardScore)
            })
            sliceArr.forEach(item => {
              item.classifyScore = classifyScore
            })
          }
        })

      },
    },
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../../common/stylus/mixin.styl"
  .examinexaminationsWrapWrap
    page1()
    background-color #EFEEF3
    .el-radio-button
      margin-right 10px
    .el-radio-button__inner
      border-radius 10px !important
      padding 5px 15px
      margin-right 10px
      border: 1px solid #dcdfe6 !important
    .is-checked
      .el-radio-button__inner
        background-color: #3975FE
        border-radius 10px !important
        border-color #3975FE


</style>
